package cn.gson.crmdemo.controller.sale;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.service.CpService;
import cn.gson.crmdemo.model.service.QuotationService;
import cn.gson.crmdemo.model.vo.QuoDetailsVo;
import cn.gson.crmdemo.model.vo.QuoSaleCusProVo;
import cn.gson.crmdemo.model.vo.SaleVo;

@Controller
public class QuotationController {
	@Autowired
	CpService cps;

	@Autowired
	QuotationService qs;

	/**
	 * 报价单查询所有
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("quo-All")
	@ResponseBody
	public Map<String, Object> selectQuotationAll(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = qs.selectQuotation();
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 查所有产品
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("pro-All")
	@ResponseBody
	public Map<String, Object> selectProductAll(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = cps.selectProduct();
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 添加报价单
	 * 
	 * @param sales
	 * @return
	 */
	@RequestMapping("add-quo")
	public String addQuo(QuoSaleCusProVo quotation) {
		qs.addQuo(quotation);
		// 重定向到查询页面
		return "redirect:quo-All?page=1&pageSize=7";
	}
	
	/**
	 * 修改报价单
	 * 
	 * @param sales
	 * @return
	 */
	@RequestMapping("update-quos")
	public String updateQuos(QuoSaleCusProVo quotation) {
		System.err.println("quotationid修改" + quotation.getQuotationId());
	
		qs.updateQuos(quotation);
		// 重定向到查询页面
		return "redirect:quo-All?page=1&pageSize=7";
	}


	/**
	 * 根据id查询报价单
	 * 
	 * @param salesId
	 * @return
	 */
	@RequestMapping("quotation-id")
	@ResponseBody
	public JSONArray selectquotationId(Integer quotationId) {
		System.err.println("id:=====================" + quotationId);
		// 当前页的内容
		JSONArray list = qs.selectquotationId(quotationId);
		return list;
	}

	/**
	 * 客户模糊查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("quo-select")
	@ResponseBody
	public Map<String, Object> selectMoHuCustomerName(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize, String quotationTheme) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = qs.selectMoHuQuotationTheme(quotationTheme);
		System.err.println("=====================quo" + quotationTheme);
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}
	
	/**
	 * 根据状态查询
	 * @param saleState
	 * @return
	 */
		@RequestMapping("quo-State")
		@ResponseBody
		public Map<String, Object> quoState(@RequestParam("page") Integer pageNum,
				@RequestParam("pageSize") Integer pageSize,Integer approvalStatus) {
			Page<Object> page = PageHelper.startPage(pageNum, pageSize);
			// 当前页的内容
			JSONArray list = qs.selectApprovalStatus(approvalStatus);
			System.err.println("=====================saleState:"+approvalStatus);
			// 总记录数
			long total = page.getTotal();
			Map<String, Object> map = new HashMap<>();
			map.put("list", list);
			map.put("total", total);
			return map;
		}
		
		// 增删改之后，一定要重定向到查询的接口
		@RequestMapping("update-State")
		public String delquo(Integer quotationId) {
			qs.updateStatus(quotationId);
			return "redirect:quo-All?page=1&pageSize=7";
		}

		/**
		 * select产品和详情
		 * @param saleState
		 * @return
		 */
			@RequestMapping("select-detailspro")
			@ResponseBody
			public Map<String, Object> selectdetailspro(@RequestParam("page") Integer pageNum,
					@RequestParam("pageSize") Integer pageSize,Integer quotationId) {
				Page<Object> page = PageHelper.startPage(pageNum, pageSize);
				// 当前页的内容
				JSONArray list = qs.selectdetailspro(quotationId);
				System.err.println("=====================quotationId:"+quotationId);
				// 总记录数
				long total = page.getTotal();
				Map<String, Object> map = new HashMap<>();
				map.put("list", list);
				map.put("total", total);
				return map;
			}
			
			@RequestMapping("select-details")
			@ResponseBody
			public JSONArray selectdetailspro2(Integer quotationId) {
				System.err.println("selectdetailspro2:=====================" + quotationId);
				// 当前页的内容
				JSONArray list = qs.selectdetailspro(quotationId);
				return list;
			}
			/**
			 * 添加报价单详情
			 * 
			 * @param sales
			 * @return
			 */
			@RequestMapping("add-details")
			public String addQuoDetails(Integer proId,Integer quoId,Integer quotationDetailsNumber ) {
				List<Integer> list  = qs.selectcpId(proId,quoId);
				if(list.size()==0) {
					
					qs.addQuoDetails(proId,quoId,quotationDetailsNumber);
				}else{
					System.err.println("================list"+list.get(0));
					qs.updateQuonum( quotationDetailsNumber,list.get(0));
				}
				// 重定向到查询页面
				return "redirect:select-details?quotationId="+quoId+"";
			}
			/**
			 * 删除销售机会
			 * @param saleId
			 * @return
			 */
				// 增删改之后，一定要重定向到查询的接口
				@RequestMapping("delete-details")
				public String deleteDetails(Integer quotationDetailsId,Integer quoId) {
					System.err.println("=======delete-details====================="+"quoId"+quoId);
					qs.deleteDetails(quotationDetailsId);
					return "redirect:select-details?quotationId="+quoId+"";
				}
				
				
}
